from flask import Flask
from flask_cors import CORS
from celery import Celery

from .api_v1 import api_v1
from .config import configs
from .utils import db, security, create_app, mail
from .utils.blue_prints import Api_v1_bp, Main_bp

__extensions__ = [db, api_v1, security, mail]
__blueprints__ = [Api_v1_bp, Main_bp]

celery = Celery()

def create_app(FLASK_CONFIG):
    app = Flask(__name__)
    app.config.from_object(configs[FLASK_CONFIG])
    configs[FLASK_CONFIG].init_app(app)
    CORS(app)
    # init blueprints
    app.register_blueprint(Api_v1_bp)
    app.register_blueprint(Main_bp)
    # init extension
    db_cxt = db.init_app(app)
    api_v1_cxt = api_v1.init_app(app)
    scr_cxt = security.init_app(app)
    mail_ctx = mail.init_app(app)
    @celery.task
    def send_flask_mail(msg):
        mail.send(msg)


    @scr_cxt.send_mail_task
    def delay_flask_security_mail(msg):
        send_flask_mail.delay(msg)
    return app


